(def collider-info "A package for producing ANSI escape sequences.")
(def . partial)
(def [ansi-pfx ansi-sep ansi-sfx ansi-6-fg ansi-6-bg ansi-8-fg ansi-8-bg]
     ["\e["    ";"      "m"      "38;5"    "48;5"    "38;2"    "48;2"   ])
(defn string/strip-ansi (s) #s
  (peg/replace-all '(sequence "\e" 1 (thru "m")) s)
  )
(defn string/fit (n s) #n,s
  (def len (length s)) #n
  (if (< n len)
    (string/slice s 0 n)
    (string s (string/repeat " " (- n len)))))
(defn string/resize (chars w) #s,n
 (var repnum
      (inc (div w
                (length chars))))
  (if (= math/inf repnum) (set repnum " ")
(string/slice
 (string/repeat chars repnum)
 0 w)))
(def string/longest #*[&s]
  (. extreme |(> (length $) (length $1)) ))
(defn string/longest-length ( & strings ) #*[&s]
  (length (string/longest strings)) )

(defn point (fn (a) (fn (& etc) a)))
#(defmacro macro-map (macro & xs) #M,l
#   (pp macro)
#   (def macro-name (symbol macro))
#   (pp macro-name)
#   (pp xs)
#   (def macro-tuple (map |~[,macro-name ,;$&] xs))
#  (pp macro-tuple)
#  [ macro-tuple ])

#(defmacro ddefn (name default-pairs & body)
#  (let (default-array (map |['default $0 $1] ;default-pairs)
#         default-body [;default-array ;body])
#  ~(defn ,name ,default-body  )))

(defn range-real (&opt x y step)
  (default step 1)
  (if (int? step) (range x y step)
      (map (. * step) (range (div x step) (div y step)))))

(defn load-file (filepath)
  (eval-string (slurp filepath)))

(defn random-int (min max)
  (math/floor (+ (* (math/random) (- max min)) min)))

(defn pseudorandom-int (min max &opt seed)
  (default seed (% (os/clock) 1024))
  (math/floor (+ min (:int (math/rng seed) (- max min)))))

(defn index-random (ds)
  (ds (get (keys ds) (random-int 0 (length ds)))))
(defn index-randoms-unique (ds n)
  (def res @[])
  (def rvl (invert res))
  (while (< (length res) n)
  (def t (random-int 0 (length ds)))
    (if (nil? (get rvl t)) (array/push res (get ds t))))
  res)

(defn shuffle (ds &opt random-seed) #IDEA: Shuffle-based compression.
  (default random-seed (os/cryptorand 4))
  (def rand (math/rng random-seed) )
  (if (indexed? ds)
    (for i 0 (- (length ds) 1)
      (def j (:int rand (length ds)))
      (def k (ds i))
      (set (ds i) (ds j))
       (set    (ds j) k ))
      ) ds)

(defn return-shell (cmd-and-args &opt inpu)
  (def [[stdin-r stdin-w] [stdout-r stdout-w]] [(os/pipe) (os/pipe)])
  (os/execute cmd-and-args :p {:out stdout-w :in stdin-r})
  (if inpu (do
             (:write stdin-w inpu)
             (:close stdin-w)))
   (def v (:read stdout-r math/int32-max))
  (map :close [stdin-r stdin-w stdout-r stdout-w])
  v)
